package com.iot.manage.dao;

import com.alibaba.fastjson.JSONObject;
import com.iot.manage.pojo.DeviceProperty;
import com.iot.manage.pojo.SysCode;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface DPropertyMapper {

    @Select("<script>" +
            "SELECT\n" +
            "temp.DPROPERTY_ID dpropertyId,\n" +
            "temp.DPROPERTY_CODE dpropertyCode,\n" +
            "temp.DPROPERTY_NAME dpropertyName,\n" +
            "dpropertyType,\n" +
            "dpropertyUnit,\n" +
            "temp.DPROPERTY_ARG dpropertyArg\n" +
            "FROM ( \n" +
            "SELECT\n" +
            "ROWNUM rowno,\n" +
            "t.*,\n" +
            "c.CODE_NAME dpropertyType,\n" +
            "u.CODE_NAME dpropertyUnit\n" +
            "FROM T_DEVICE_PROPERTY t \n" +
            "LEFT JOIN (SELECT * FROM T_SYS_CODE WHERE CODE_TYPE='属性类型') c ON t.DPROPERTY_TYPE = c.CODE_VALUE\n" +
            "LEFT JOIN (SELECT * FROM T_SYS_CODE WHERE CODE_TYPE='属性单位') u ON t.DPROPERTY_UNIT = u.CODE_VALUE\n" +
            "WHERE (t.DPROPERTY_NAME LIKE CONCAT(CONCAT('%', #{query}),'%') OR t.DPROPERTY_CODE LIKE CONCAT(CONCAT('%',#{query}),'%')) AND ROWNUM &lt;= #{pagesize} ORDER BY t.DPROPERTY_ID ) temp \n" +
            "WHERE temp.rowno &gt; #{start}" +
            "</script>")
    List<DeviceProperty> findDPropertyList(@Param("query") String query, @Param("start") int start, @Param("pagesize") int pagesize);

    @Select("<script>SELECT COUNT(a.DPROPERTY_ID) FROM T_DEVICE_PROPERTY a WHERE a.DPROPERTY_CODE LIKE CONCAT(CONCAT('%', #{query}),'%') OR a.DPROPERTY_NAME LIKE CONCAT(CONCAT('%', #{query}),'%')</script>")
    int countDPropertyList(String query);

    @Insert("<script>" +
            "INSERT INTO T_DEVICE_PROPERTY \n" +
            "(DPROPERTY_ID,DPROPERTY_CODE,DPROPERTY_NAME,DPROPERTY_TYPE,DPROPERTY_UNIT" +
            "<if test=\" dpropertyArg != null\">"+
            ",DPROPERTY_ARG" +
            "</if>"+
            ") \n" +
            "VALUES \n" +
            "(#{dpropertyId},#{dpropertyCode},#{dpropertyName},#{dpropertyType},#{dpropertyUnit}" +
            "<if test=\" dpropertyArg != null\">"+
            ",#{dpropertyArg}" +
            "</if>"+
            ")" +
            "</script>")
    void addDevicePropertyInfo(JSONObject params);

    @Select("<script>" +
            "SELECT  \n" +
            " temp.DPROPERTY_ID dpropertyId,  \n" +
            " temp.DPROPERTY_CODE dpropertyCode,  \n" +
            " temp.DPROPERTY_NAME dpropertyName,\n" +
            " temp.DPROPERTY_TYPE dpropertyType,\n" +
            " temp.DPROPERTY_UNIT dpropertyUnit,\n" +
            " temp.DPROPERTY_ARG dpropertyArg\n" +
            "FROM T_DEVICE_PROPERTY temp\n" +
            "\tWHERE temp.DPROPERTY_ID = #{params} OR temp.DPROPERTY_CODE = #{params}\n" +
            "</script>")
    DeviceProperty findDevicePropertyInfoByIdOrCode(String params);

    @Update("<script>" +
            "UPDATE T_DEVICE_PROPERTY SET \n" +
            "\tDPROPERTY_CODE = #{dpropertyCode},\n" +
            "\tDPROPERTY_NAME = #{dpropertyName},\n" +
            "\tDPROPERTY_TYPE = #{dpropertyType},\n" +
            "\tDPROPERTY_UNIT = #{dpropertyUnit}\n" +
            "<if test=\" dpropertyArg != null\">"+
            "\t,DPROPERTY_ARG = #{dpropertyArg}\n" +
            "</if>"+
            "WHERE DPROPERTY_ID = #{dpropertyId}" +
            "</script>")
    void updateDevicePropertyInfo(JSONObject params);

    @Delete("<script>DELETE FROM T_DEVICE_PROPERTY WHERE DPROPERTY_ID = #{dpropertyId}</script>")
    void deleteDPropertyInfo(String dpropertyId);

    @Select("<script>SELECT a.CODE_VALUE codeValue, a.CODE_NAME codeName FROM T_SYS_CODE a WHERE a.CODE_TYPE='属性类型' ORDER BY a.CODE_VALUE</script>")
    List<SysCode> findDPropertyTypeList();

    @Select("<script>SELECT a.CODE_VALUE codeValue, a.CODE_NAME codeName FROM T_SYS_CODE a WHERE a.CODE_TYPE='属性单位' ORDER BY a.CODE_VALUE</script>")
    List<SysCode> findDPropertyUnitList();

    @Select("<script>SELECT count(MODEL_ID) FROM T_DEVICE_MODEL WHERE MODEL_ARG LIKE CONCAT(CONCAT('%', CONCAT(CONCAT('\"', #{query}),'\"')),'%')</script>")
    int countPropertyUseTotal(String propertyCode);

    @Update("<script>update T_DEVICE_MODEL set MODEL_ARG = replace(MODEL_ARG,CONCAT(CONCAT('\"', #{oldDevicePropertyCode}),'\"'),CONCAT(CONCAT('\"', #{dpropertyCode}),'\"'))</script>")
    void updateDeviceModelArg(JSONObject params);
}
